package com.xbongbong.pro.message.pushset;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.message.base.AbstractPushSet;
import com.xbongbong.pro.message.constant.PushSetConstant;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushTimeTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.model.PushSetModel;
import com.xbongbong.saas.constant.StringConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author chy
 *  Modified by: 魏荣杰 2018/7/25 10:42
 */
public class DistributorPushSet extends AbstractPushSet {

    private static Integer oneDay = 86400;

    private String pushObject;



    public String getPushObject() {
        return pushObject;
    }



    public void setPushObject(String pushObject) {
        this.pushObject = pushObject;
    }

    @Override
    public PushSetEntity toPushSetEntity(String pushObject, String pushTime,String attrEntity) {
        setPushObject(pushObject);
        return new PushSetEntity(){{
            setAddTime(addTime);
            setAttrEntity(attrEntity);
            setCorpid(corpid);
            setDel(del);
            setEnable(enable);
            setId(id);
            setPushObject(pushObject);
            setPushTime(pushTime);
            setRefEntity(refEntity);
            setSubType(subType);
            setType(type);
            setUpdateTime(updateTime);
        }};
    }



    @Override
    public PushSetEntity getPushSet(String corpid, Integer subType,PushSetModel pushSetModel) {
        Integer type = PushTypeEnum.DISTRIBUTOR_PUSH.getCode();

        Map<String, Object> param = new HashMap<String, Object>(16) {{
            put("corpid", corpid);
            put("type", type);
            put("subType", subType);
        }};

        List<PushSetEntity> pushSetList = pushSetModel.findEntitys(param);
        PushSetEntity pushSetEntity;
        if (pushSetList.isEmpty()) {
            pushSetEntity = new PushSetEntity() {{
                setType(type);
                setSubType(subType);
            }};

            if (Objects.equals(PushTypeEnum.DISTRIBUTOR_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setEnable(0);
            } else {
                pushSetEntity.setEnable(1);
            }

            JSONObject pushObject = new JSONObject();
            if (Objects.equals(PushTypeEnum.DISTRIBUTOR_ADD_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_ADD_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_ADD));
                //type
                pushObject.put("type", new JSONArray() {{
                    add(new JSONObject() {{
                        put("name", PushObjectTypeEnum.MAINUSER.getName());
                        put("enable", 0);
                        put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                    }});
                }});

                //manager
                pushObject.put("manager", new JSONObject() {{
                    put("enable", 1);
                    put("value", new JSONArray() {{
                        add(new JSONObject() {{
                            put("id", "1");
                            put("name", I18nMessageUtil.getMessage(CommonConstant.FIRST_LEVEL_SUPERVISOR));
                        }});
                    }});
                }});

                //dep
                pushObject.put("dep", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //role
                pushObject.put("role", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //user
                pushObject.put("user", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});
            } else if (Objects.equals(PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_MAIN.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_MAIN.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_TEAM_ADD_MAIN));
                //type
                pushObject.put("type", new JSONArray() {{
                    add(new JSONObject() {{
                        put("name", PushObjectTypeEnum.MAINUSER.getName());
                        put("enable", 0);
                        put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                    }});
                    add(new JSONObject() {{
                        put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                        put("enable", 0);
                        put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                    }});
                }});

                //manager
                pushObject.put("manager", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //dep
                pushObject.put("dep", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //role
                pushObject.put("role", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //user
                pushObject.put("user", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

            } else if (Objects.equals(PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_SYNERGETIC.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_SYNERGETIC.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_TEAM_ADD_SYNERGETIC));
                //type
                pushObject.put("type", new JSONArray() {{
                    add(new JSONObject() {{
                        put("name", PushObjectTypeEnum.MAINUSER.getName());
                        put("enable", 0);
                        put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                    }});
                    add(new JSONObject() {{
                        put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                        put("enable", 0);
                        put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                    }});
                }});

                //manager
                pushObject.put("manager", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //dep
                pushObject.put("dep", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //role
                pushObject.put("role", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //user
                pushObject.put("user", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

//            } else if (Objects.equals(PushTypeEnum.DISTRIBUTOR_DISTRIBUTION_PUSH.getSubCode(), subType)) {
//                pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_DISTRIBUTION_PUSH.getName());
//                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_DISTRIBUTION));
//                //type
//                pushObject.put("type", new JSONArray() {{
//                    add(new JSONObject() {{
//                        put("name", PushObjectTypeEnum.MAINUSER.getName());
//                        put("enable", 0);
//                        put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
//                    }});
//                    add(new JSONObject() {{
//                        put("name", PushObjectTypeEnum.COLLABORATOR.getName());
//                        put("enable", 0);
//                        put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
//                    }});
//                    add(new JSONObject() {{
//                        put("name", PushObjectTypeEnum.TODISTRBUTION.getName());
//                        put("enable", 1);
//                        put("alias", PushObjectTypeEnum.TODISTRBUTION.getAlias());
//                        put("editUnable", 1);
//                    }});
//                }});
//
//                //manager
//                pushObject.put("manager", new JSONObject() {{
//                    put("enable", 0);
//                    put("value", new ArrayList<>());
//                }});
//
//                //dep
//                pushObject.put("dep", new JSONObject() {{
//                    put("enable", 0);
//                    put("value", new ArrayList<>());
//                }});
//
//                //role
//                pushObject.put("role", new JSONObject() {{
//                    put("enable", 0);
//                    put("value", new ArrayList<>());
//                }});
//
//                //user
//                pushObject.put("user", new JSONObject() {{
//                    put("enable", 0);
//                    put("value", new ArrayList<>());
//                }});

            } else if (Objects.equals(PushTypeEnum.DISTRIBUTOR_HANDOVER_PUSH.getSubCode(), subType)) {
                pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_HANDOVER_PUSH.getName());
                pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_HANDOVER));
                //type
                pushObject.put("type", new JSONArray() {{
                    add(new JSONObject() {{
                        put("name", PushObjectTypeEnum.MAINUSER.getName());
                        put("enable", 0);
                        put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                    }});
                    add(new JSONObject() {{
                        put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                        put("enable", 0);
                        put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                    }});
                    add(new JSONObject() {{
                        put("name", PushObjectTypeEnum.FROMHANDOVER.getName());
                        put("alias", PushObjectTypeEnum.FROMHANDOVER.getAlias());
                        put("enable", 1);
                        put("editUnable", 1);
                    }});
                }});

                //manager
                pushObject.put("manager", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //dep
                pushObject.put("dep", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //role
                pushObject.put("role", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});

                //user
                pushObject.put("user", new JSONObject() {{
                    put("enable", 0);
                    put("value", new ArrayList<>());
                }});
            }
            pushSetEntity.setPushObject(pushObject.toJSONString());
        } else {
                pushSetEntity = pushSetList.get(0);
                if (Objects.equals(PushTypeEnum.DISTRIBUTOR_ADD_PUSH.getSubCode(), subType)) {
                    pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_ADD_PUSH.getName());
                    pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_ADD));

                } else if (Objects.equals(PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_MAIN.getSubCode(), subType)) {
                    pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_MAIN.getName());
                    pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_TEAM_ADD_MAIN));

                } else if (Objects.equals(PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_SYNERGETIC.getSubCode(), subType)) {
                    pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_TEAM_ADD_DEL_SYNERGETIC.getName());
                    pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_TEAM_ADD_SYNERGETIC));

//                } else if (Objects.equals(PushTypeEnum.DISTRIBUTOR_DISTRIBUTION_PUSH.getSubCode(), subType)) {
//                    pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_DISTRIBUTION_PUSH.getName());
//                    pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_DISTRIBUTION));

                } else if (Objects.equals(PushTypeEnum.DISTRIBUTOR_HANDOVER_PUSH.getSubCode(), subType)) {
                    pushSetEntity.setTitle(PushTypeEnum.DISTRIBUTOR_HANDOVER_PUSH.getName());
                    pushSetEntity.setContent(I18nMessageUtil.getMessage(PushSetConstant.DISTRIBUTOR_HANDOVER));
                }
            }
            return pushSetEntity;
    }
    private JSONArray formatPushSetType(String typeString, PushTypeEnum pushTypeEnum) {
        JSONArray typeArray = new JSONArray();
        switch (pushTypeEnum) {
            case DISTRIBUTOR_ADD_PUSH:
                typeArray.add(new JSONObject(){{
                    put("name", PushObjectTypeEnum.MAINUSER.getName());
                    if (typeString.contains(StringConstant.VERTICAL_LINE + PushObjectTypeEnum.MAINUSER.getCode() + StringConstant.VERTICAL_LINE)) {
                        put("enable", 1);
                    } else {
                        put("enable", 0);
                    }
                    put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                }});
                typeArray.add(new JSONObject(){{
                    put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                    if (typeString.contains(StringConstant.VERTICAL_LINE + PushObjectTypeEnum.COLLABORATOR.getCode() + StringConstant.VERTICAL_LINE)) {
                        put("enable", 1);
                    } else {
                        put("enable", 0);
                    }
                    put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                }});
                break;
            case DISTRIBUTOR_TEAM_ADD_DEL_MAIN:
                break;
            case DISTRIBUTOR_TEAM_ADD_DEL_SYNERGETIC:
                break;
//            case DISTRIBUTOR_DISTRIBUTION_PUSH:
//                //分配人
//                typeArray.add(new JSONObject(){{
//                    put("name", PushObjectTypeEnum.TODISTRBUTION.getName());
//                    put("enable", 1);
//                    put("alias", PushObjectTypeEnum.TODISTRBUTION.getAlias());
//                    //不允许修改
//                    put("editUnable", 1);
//                }});
//
//                typeArray.add(new JSONObject(){{
//                    put("name", PushObjectTypeEnum.MAINUSER.getName());
//                    put("enable", 0);
//                    put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
//                }});
//
//                typeArray.add(new JSONObject(){{
//                    put("name", PushObjectTypeEnum.COLLABORATOR.getName());
//                    if (typeString.contains(StringConstant.VERTICAL_LINE + PushObjectTypeEnum.COLLABORATOR.getCode() + StringConstant.VERTICAL_LINE)) {
//                        put("enable", 1);
//                    } else {
//                        put("enable", 0);
//                    }
//                    put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
//                }});
//                break;
            case DISTRIBUTOR_HANDOVER_PUSH:
                typeArray.add(new JSONObject(){{
                    //移交人
                    put("name", PushObjectTypeEnum.TOHANDOVER.getName());
                    put("enable", 1);
                    put("alias", PushObjectTypeEnum.TOHANDOVER.getAlias());
                    put("editUnable", 1);
                }});
                typeArray.add(new JSONObject(){{
                    //被移交人
                    put("name", PushObjectTypeEnum.FROMHANDOVER.getName());
                    put("enable", 1);
                    put("alias", PushObjectTypeEnum.FROMHANDOVER.getAlias());
                    put("editUnable", 1);
                }});

                typeArray.add(new JSONObject(){{
                    put("name", PushObjectTypeEnum.MAINUSER.getName());
                    put("enable", 0);
                    put("alias", PushObjectTypeEnum.MAINUSER.getAlias());
                }});

                typeArray.add(new JSONObject(){{
                    put("name", PushObjectTypeEnum.COLLABORATOR.getName());
                    put("enable", 0);
                    put("alias", PushObjectTypeEnum.COLLABORATOR.getAlias());
                }});

                break;
            default:
                break;
        }
        return typeArray;
    }

    @Override
    public JSONObject getTimeJson(JSONObject jsonObject) {
        JSONObject object = new JSONObject();
        if (!jsonObject.isEmpty()) {
            object.put("period", jsonObject.getInteger("period") != null ? jsonObject.getInteger("period") : DistributorPushSet.oneDay);
            object.put("timeType", jsonObject.getInteger("timeType") != null ? jsonObject.getInteger("timeType") : PushTimeTypeEnum.ADVANCE.getCode());
            object.put("loop", jsonObject.getInteger("loop") != null ? jsonObject.getInteger("loop") : 0);
            object.put("time", jsonObject.getInteger("time") != null ? jsonObject.getInteger("time") : 0);
            object.put("type", jsonObject.getInteger("type") != null ? jsonObject.getInteger("type") : 1);
        } else {
            object = new JSONObject();
        }
        return object;
    }



    @Override
    public PushSetEntity getPushSet(String corpid, String attr, PushSetModel pushSetModel) {
        return null;
    }

    @Override
    public List<PushSetEntity> getPushSet(String corpid, List<String> attrList, PushSetModel pushSetModel, Long templateId) {
        return null;
    }

    public static void main(String[] args) {

    }
}
